﻿namespace WMS6.Web.Administration
{
    using System;
    using System.Collections.Generic;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.BaseInfoServiceRef;

    public class AddWarehouse : BasePage
    {
        protected Button btnCopy;
        protected Button btnDelete;
        protected Button btnGoBack;
        protected Button btnNew;
        protected Button btnSave;
        protected CheckBox chkIsActive;
        protected HtmlForm F1066;
        protected HtmlInputHidden hdnDbConfigID;
        protected HtmlInputHidden hdnFlag;
        protected HtmlInputHidden hdnFrom;
        protected HtmlHead Head1;
        protected Label lblAddress;
        protected Label lblAllocateRule;
        protected Label lblCity;
        protected Label lblContact1;
        protected Label lblContact2;
        protected Label lblCountry;
        protected Label lblDbLoginID;
        protected Label lblDbLoginPasswd;
        protected Label lblEmail1;
        protected Label lblEmail2;
        protected Label lblFax1;
        protected Label lblFax2;
        protected Label lblMsg;
        protected Label lblPhone1;
        protected Label lblPhone2;
        protected Label lblProvince;
        protected Label lblWbDbDescr;
        protected Label lblWhDbName;
        protected Label lblZip;
        protected HtmlLink mycss;
        protected Label one1;
        protected Label one2;
        protected ScriptManager ScriptManager1;
        protected TextBox txtAddress;
        protected TextBox txtCity;
        protected TextBox txtConatact2;
        protected TextBox txtContact1;
        protected TextBox txtCountry;
        protected TextBox txtDbLoginID;
        protected TextBox txtDbLoginPasswd;
        protected TextBox txtEmail1;
        protected TextBox txtEmail2;
        protected TextBox txtFax1;
        protected TextBox txtFax2;
        protected TextBox txtPhone1;
        protected TextBox txtPhone2;
        protected TextBox txtProvince;
        protected TextBox txtWbDbDescr;
        protected TextBox txtWhDbName;
        protected TextBox txtZIP;
        protected UpdatePanel up1;

        protected void btnCopy_Click(object sender, EventArgs e)
        {
            this.hdnDbConfigID.Value = "";
            this.txtWhDbName.Text = "";
            this.txtWhDbName.Enabled = true;
            this.hdnFlag.Value = "0";
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(this.hdnDbConfigID.Value))
            {
                try
                {
                    List<string> DbConfigID = new List<string> {
                        this.hdnDbConfigID.Value
                    };
                    this.lblMsg.Text = BaseInfoServiceClientProxy.GetInstance().Delete(DbConfigID, "DB_CONFIG") + base.GetGlobalMsgResource("F1650_005");
                    this.btnSave.Enabled = false;
                    this.btnDelete.Enabled = false;
                    this.btnCopy.Enabled = false;
                    base.DataEntityViewState = null;
                }
                catch (Exception ex)
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("F1650_006") + ex.Message;
                }
            }
        }

        protected void btnGoBack_Click(object sender, EventArgs e)
        {
            switch (this.hdnFrom.Value)
            {
                case "query":
                    base.Response.Redirect("AddWarehouseQuery.aspx", false);
                    break;

                case "list":
                    base.Response.Redirect("AddWarehouseList.aspx?from=edit", false);
                    break;
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.txtWhDbName.Text.Trim()))
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F1650_004");
            }
            else if (this.hdnFlag.Value == "1")
            {
                this.UpdateDbConfigInfo();
            }
            else
            {
                this.InsertDbConfigInfo();
            }
        }

        private void InsertDbConfigInfo()
        {
            try
            {
                DbConfigInfo DbConfig = new DbConfigInfo {
                    WhDbName = this.txtWhDbName.Text.Trim(),
                    WhDbDesc = this.txtWbDbDescr.Text.Trim(),
                    IsActive = this.chkIsActive.Checked ? "Y" : "N",
                    DbLoginID = this.txtDbLoginID.Text,
                    DbLoginPasswd = this.txtDbLoginPasswd.Text,
                    CreatedBy = base.UserInfo.UserID,
                    DbDbms = "",
                    IsGlobal = "N",
                    City = this.txtCity.Text.Trim(),
                    State = this.txtProvince.Text.Trim(),
                    Zip = this.txtZIP.Text.Trim(),
                    Country = this.txtCountry.Text.Trim(),
                    Contact1 = this.txtContact1.Text.Trim(),
                    Contact2 = this.txtConatact2.Text.Trim(),
                    Phone1 = this.txtPhone1.Text.Trim(),
                    Phone2 = this.txtPhone2.Text.Trim(),
                    Fax1 = this.txtFax1.Text.Trim(),
                    Fax2 = this.txtFax2.Text.Trim(),
                    Email1 = this.txtEmail1.Text.Trim(),
                    Email2 = this.txtEmail2.Text.Trim(),
                    Address = this.txtAddress.Text.Trim()
                };
                this.lblMsg.Text = BaseInfoServiceClientProxy.GetInstance().Insert(DbConfig, "DB_CONFIG") + base.GetGlobalMsgResource("F1650_001");
                base.DataEntityViewState = DbConfig;
                this.hdnFlag.Value = "1";
                this.txtWhDbName.Enabled = false;
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F1650_003") + ex.Message;
            }
        }

        private void LoadData()
        {
            if (!string.IsNullOrEmpty(base.Request.QueryString["ID"]))
            {
                this.hdnDbConfigID.Value = base.Request.QueryString["ID"].ToString();
                this.SetDbConfigForm();
                this.hdnFlag.Value = "1";
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["detail"]))
            {
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'>setTab('one',1,2);</script>", false);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
            }
            this.btnCopy.Visible = false;
            this.btnNew.Visible = false;
            this.btnDelete.Visible = false;
        }

        private void SetDbConfigForm()
        {
            try
            {
                string whdbid = this.hdnDbConfigID.Value;
                DbConfigInfo DbConfig = BaseInfoServiceClientProxy.GetInstance().GetInfoByID(whdbid, "DB_CONFIG") as DbConfigInfo;
                base.DataEntityViewState = DbConfig;
                if (DbConfig != null)
                {
                    this.txtWhDbName.Text = DbConfig.WhDbName;
                    this.txtWbDbDescr.Text = DbConfig.WhDbDesc;
                    this.txtDbLoginID.Text = DbConfig.DbLoginID;
                    this.txtDbLoginPasswd.Text = DbConfig.DbLoginPasswd;
                    this.chkIsActive.Checked = DbConfig.IsActive == "Y";
                    this.txtWhDbName.Enabled = false;
                    this.txtCity.Text = DbConfig.City;
                    this.txtProvince.Text = DbConfig.State;
                    this.txtZIP.Text = DbConfig.Zip;
                    this.txtCountry.Text = DbConfig.Country;
                    this.txtContact1.Text = DbConfig.Contact1;
                    this.txtConatact2.Text = DbConfig.Contact2;
                    this.txtPhone1.Text = DbConfig.Phone1;
                    this.txtPhone2.Text = DbConfig.Phone2;
                    this.txtFax1.Text = DbConfig.Fax1;
                    this.txtFax2.Text = DbConfig.Fax2;
                    this.txtEmail1.Text = DbConfig.Email1;
                    this.txtEmail2.Text = DbConfig.Email2;
                    this.txtAddress.Text = DbConfig.Address;
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        private void UpdateDbConfigInfo()
        {
            try
            {
                DbConfigInfo DbConfig = base.DataEntityViewState as DbConfigInfo;
                DbConfig.WhDbID = int.Parse(this.hdnDbConfigID.Value);
                DbConfig.WhDbName = this.txtWhDbName.Text.Trim();
                DbConfig.WhDbDesc = this.txtWbDbDescr.Text;
                DbConfig.DbLoginID = this.txtDbLoginID.Text;
                DbConfig.DbLoginPasswd = this.txtDbLoginPasswd.Text;
                DbConfig.IsActive = this.chkIsActive.Checked ? "Y" : "N";
                DbConfig.DbDbms = "";
                DbConfig.IsGlobal = "N";
                DbConfig.UpdatedBy = base.UserInfo.UserID;
                DbConfig.City = this.txtCity.Text.Trim();
                DbConfig.State = this.txtProvince.Text.Trim();
                DbConfig.Zip = this.txtZIP.Text.Trim();
                DbConfig.Country = this.txtCountry.Text.Trim();
                DbConfig.Contact1 = this.txtContact1.Text.Trim();
                DbConfig.Contact2 = this.txtConatact2.Text.Trim();
                DbConfig.Phone1 = this.txtPhone1.Text.Trim();
                DbConfig.Phone2 = this.txtPhone2.Text.Trim();
                DbConfig.Fax1 = this.txtFax1.Text.Trim();
                DbConfig.Fax2 = this.txtFax2.Text.Trim();
                DbConfig.Email1 = this.txtEmail1.Text.Trim();
                DbConfig.Email2 = this.txtEmail2.Text.Trim();
                DbConfig.Address = this.txtAddress.Text.Trim();
                this.lblMsg.Text = BaseInfoServiceClientProxy.GetInstance().Update(DbConfig, "DB_CONFIG") + base.GetGlobalMsgResource("F1650_001");
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F1650_002") + ex.Message;
            }
        }
    }
}

